package com.yiren.backstage.player.utils;

import android.content.Context;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Time:2022/3/9 19:00
 * Author:xiaozan
 * Description:工具类-lame媒体文件处理
 */
public class LameUtil {

    /*** The tag. */
    private static String TAG = "LAME工具类";

    /*** 上下文. */
    private Context context;

    /*** 处理进度. */
    private int pregress;

    /**
     * Instantiates a new lame util.
     *
     * @param context the context
     */
    public LameUtil(Context context) {
        super();
        this.context = context;
    }

    /**
     * Pcm2wav.
     *
     * @param pcm the pcm
     * @param wav the wav
     * @return true, if successful
     */
    public boolean pcm2wav(String pcm, String wav) {
        byte[] pcmByte = file2byte(pcm);
        byte[] file = new byte[0];
        byte[] biaot = "RIFF".getBytes();
        file = wavHeadWith(file, biaot);
        byte[] wjcd = int2byte(pcmByte.length + 36);
        file = wavHeadWith(file, wjcd);
        byte[] wjsx = "WAVEfmt ".getBytes();
        file = wavHeadWith(file, wjsx);
        byte[] wjsx1 = {16, 0, 0, 0, 1, 0, 1, 0, (byte) 128, 62, 0, 0, 0, 125, 0, 0, 2, 0, 16, 0};
        file = wavHeadWith(file, wjsx1);
        byte[] wjsx2 = "data".getBytes();
        file = wavHeadWith(file, wjsx2);
        byte[] sjcd = int2byte(pcmByte.length);
        file = wavHeadWith(file, sjcd);
        file = wavHeadWith(file, pcmByte);
        return writeFile(wav, file);
    }

    /**
     * Write file.
     *
     * @param mbwj the mbwj
     * @param bs   the bs
     * @return true, if successful
     */
    public boolean writeFile(String mbwj, byte[] bs) {
        File destFile = new File(mbwj);
        try {
            if (destFile.exists()) {
                destFile.delete();
            }
			/*if (!destFile.createNewFile())
				return false;// 判断是否是文件*/
            FileOutputStream fos = new FileOutputStream(destFile);
            fos.write(bs, 0, bs.length);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * File2byte.
     *
     * @param fileName the file name
     * @return the byte[]
     */
    public byte[] file2byte(String fileName) {
        byte[] buffer = new byte[0];
        try {
            @SuppressWarnings("resource")
            FileInputStream fin = new FileInputStream(fileName);
            int length = fin.available();
            buffer = new byte[length];
            fin.read(buffer);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return buffer;
    }

    /**
     * Wav head with.
     *
     * @param a the a
     * @param b the b
     * @return the byte[]
     */
    public byte[] wavHeadWith(byte[] a, byte[] b) {
        int ac = a.length, bc = b.length, zc = ac + bc;
        byte[] fh = new byte[zc];
        for (int i = 0; i < ac; i++)
            fh[i] = a[i];
        for (int i = 0; i < bc; i++)
            fh[i + ac] = b[i];
        return fh;
    }

    /**
     * Int2byte.
     *
     * @param n the n
     * @return the byte[]
     */
    private static byte[] int2byte(int n) {
        byte[] b = new byte[4];
        for (int i = 0; i < 4; i++) {
            b[i] = (byte) (n >> (24 - i * 8));
        }
        byte[] c = new byte[4];
        c[3] = b[0];
        c[2] = b[1];
        c[1] = b[2];
        c[0] = b[3];
        return c;
    }

    /**
     * 设置进度条的进度，提供给C语言调用.
     *
     * @param progress the new convert progress
     */
    public void setConvertProgress(int progress) {
        this.pregress = progress;
    }

    /**
     * 取值- 处理进度.
     *
     * @return the 处理进度
     */
    public int getPregress() {
        return pregress;
    }

    /**
     * 赋值- 处理进度.
     *
     * @param pregress the new 处理进度
     */
    public void setPregress(int pregress) {
        this.pregress = pregress;
    }

}